﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Variable Threshold
    /// </summary>
    public class VariableThreshold : BaseForeBackgroundColorFilter
    {
        protected int m_nR = 127;
        protected int m_nG = 127;
        protected int m_nB = 127;
        protected string m_sFormula = string.Empty;
        protected bool m_bKeepColors = false;

        public VariableThreshold()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_nR"></param>
        /// <param name="_nG"></param>
        /// <param name="_nB"></param>
        /// <param name="_sFormula"></param>
        public VariableThreshold(bool _bKeepColors, int _nR, int _nG, int _nB, string _sFormula)
        {
            m_bKeepColors = _bKeepColors;
            m_nR = _nR;
            m_nG = _nG;
            m_nB = _nB;
            m_sFormula = _sFormula;
        }

        public int R
        {
            get { return m_nR; }
            set { m_nR = value; }
        }

        public int G
        {
            get { return m_nG; }
            set { m_nG = value; }
        }

        public int B
        {
            get { return m_nB; }
            set { m_nB = value; }
        }

        public string Formula
        {
            get { return m_sFormula; }
            set { m_sFormula = value; }
        }

        public bool KeepColors
        {
            get { return m_bKeepColors; }
            set { m_bKeepColors = value; }
        }

        /// <summary>
        /// Variable Threshold used to clone
        /// </summary>
        /// <param name="_variableThreshold"></param>
        internal VariableThreshold(VariableThreshold _variableThreshold)
        {
            m_nR = _variableThreshold.m_nR;
            m_nG = _variableThreshold.m_nG;
            m_nB = _variableThreshold.m_nB;
            m_sFormula = _variableThreshold.m_sFormula;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;
            int x = 0;
            int y = 0;

            Color color = Color.Transparent;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = _bitmap.GetPixel(x, y);
                    if (m_bKeepColors)
                    {
                        _bitmap.SetPixel(x, y, color.R > m_nR && color.G > m_nG && color.B > m_nB ? color : this.m_ColorBackground);
                    }
                    else
                    {
                        _bitmap.SetPixel(x, y, color.R > m_nR && color.G > m_nG && color.B > m_nB ? this.m_ColorForeground : this.m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Used to clone this instance
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new VariableThreshold(this);
        }
    }
}
